package Dao.Imp;

import Dao.PersonalInfoDao;
import Response.Follow;
import modal.User;
import modal.UserApplication;
import modal.UserFollow;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Charlotte on 2017/8/23.
 */
@Repository
@Transactional
public class PersonalInfoDaoImp implements PersonalInfoDao {

    @Autowired
    SessionFactory sessionFactory;

    public String updateMyInfo(User user) {

        Session session = sessionFactory.getCurrentSession();

        try
        {
            StringBuilder sql = new StringBuilder();
            String password = user.getPassword();
            String phone = user.getPhone();
            String email= user.getEmail();
            String addr = user.getAddr();
            String id = user.getId();
            String imagesrc = user.getImagesrc();
            sql.append("UPDATE person_info SET `PASSWORD`= ?,PHONE=?,EMAIL=?,ADDR=?,imagesrc=? WHERE ID =?");
            Query query = session.createSQLQuery(sql.toString());
            query.setString(0,password);
            query.setString(1,phone);
            query.setString(2,email);
            query.setString(3,addr);
            query.setString(4,imagesrc);
            query.setString(5,id);
            if(query.executeUpdate()==1)
            {
                return "success";
            }
        }catch (Exception e)
        {
            System.err.println("更新时发生错误");
            e.printStackTrace();
        }
        return  "error";
    }

    public User getOneUser(String username) {
        Session session = sessionFactory.getCurrentSession();
        try
        {
            StringBuilder sql = new StringBuilder();
            sql.append("SELECT * from person_info WHERE USERNAME = ?");
            Query query = session.createSQLQuery(sql.toString()).setString(0,username);
            query.setResultTransformer(Transformers.aliasToBean(User.class));
            return (User)query.uniqueResult();
        }catch (Exception e)
        {
            System.out.println("获取实体失败");
            e.printStackTrace();
        }
        return null;
    }

    public List<Follow> getMyFollow(String username) {
        Session session =sessionFactory.getCurrentSession();
        try
        {
            StringBuilder sql = new StringBuilder();
            sql.append("select a.*, b.status from user_follow as a,room_master as b where  a.roommaster=b.username and a.username = ?");
            Query query = session.createSQLQuery(sql.toString()).setString(0,username);
            query.setResultTransformer(Transformers.aliasToBean(Follow.class));
            List<Follow> list = query.list();
            return list;
        }catch (Exception e)
        {
            System.out.println("获取列表失败");
            e.printStackTrace();
        }
        return null;
    }

    public String deleteMyFollow(String username, String roomid) {
        Session session = sessionFactory.getCurrentSession();
        try
        {
            StringBuilder sql = new StringBuilder();
            sql.append("DELETE FROM user_follow WHERE username = ? AND roomid=?");
            Query query = session.createSQLQuery(sql.toString());
            query.setString(0,username);
            query.setString(1,roomid);
            if(query.executeUpdate()==1)
            {
                return "success";
            }
        }catch (Exception e)
        {
            System.out.println("取消关注失败");
            e.printStackTrace();
        }
        return "error";
    }

    public String submitApplication(UserApplication application) {
        Session session = sessionFactory.getCurrentSession();

        try
        {
            application.setResult(0);
            StringBuilder sql = new StringBuilder();
            sql.append("INSERT INTO user_application (username ,realname,idcard,research,reason,result) VALUES\n" +
                    "(?,?,?,?,?,?)");
            Query query = session.createSQLQuery(sql.toString());
            query.setString(0,application.getUsername());
            query.setString(1,application.getRealname());
            query.setString(2,application.getIdcard());
            query.setString(3,application.getMajor());
            query.setString(4,application.getReason());
            query.setInteger(5,application.getResult());
            if(query.executeUpdate()==1)
            {
                return  "success";
            }
        }catch (Exception e)
        {
            System.err.println("提交申请失败");
            e.printStackTrace();
        }
        return "error";
    }
}

